/**
 * 供应商耗材维护功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "sysmanMain","bizProto","btnServ","uploadfiles","panelUtil","buttonEvent"],
		function($,datagridServ, msgServ, ajaxServ, formServ,sysmanMain, bizProto,btnServ,uploadfiles,panelUtil,buttonEvent) {

		'use strict';
		
		//根节点id
		var rootid ='div-supply-consumable';
		
		//页面宽度和高度
		var pageWidth,pageHeight,gridHeight;
		//窗口id
		var window_id = 'win-supply-consumable';
		
		var bizcode = 'supplyConsumable',fullBizcode,rootpath;
		
		//选定类别
		var selectedType;
		
		//分类查询url
		var typeQryUrl = '/supplyBasetype/query/getConbinTree.action';
		
		var detailQryUrl = '/supplyConsumable/query/getListDomain.action';
		
		var detailPageUrl = '/supplyConsumable/query/editPage.action';
		
		var detailSaveUrl = '/supplyConsumable/save.action';
		
		var detailDelUrl = '/supplyConsumable/del.action';
		
		//当前选中树节点
		var curTreeNode;
		
		var public_method = {
			initList:function(){
				
				curTreeNode = null;
				private_method.init();
				private_method.initWH();
				private_method.initTree();
				
				private_method.initDetail();
				private_method.initDetailBtn();
				
			},
			initEdit:function(){
				private_method.initEdit();
			}
		}
		
		var btnEvent = {
			// 新增函数
			add : function() {
				
				//判断是否点击供应商节点
				if(curTreeNode &&  curTreeNode.nodeType=='L'){
					var $dg= $('#'+ rootid +" .dg-detail");	
					$($dg).datagrid('clearSelections')
					btnEvent.editExecute(false);
				}else{
					msgServ.Msgslide("请选择供应商");
				}
			},
			edit:function(){
				btnEvent.editExecute(true);
			},
			// 编辑,flag 为form按钮可见性标志
			editExecute : function(flag) {	
				
				var $grid= $('#'+ rootid +" .dg-detail");	
					
				var	$edit_window = $('#' +window_id);
					
				var _url = '/'+sysmanMain.getRootPath()+detailPageUrl				
					
				var _param = {grid:$grid,window:$edit_window,href:_url,flag:flag,title:'耗材明细'}
					
				datagridServ.windowEditCenter(_param);	
				
			},
			save:function(){
					
				var $form = $('#'+window_id +' .main-form')
				
				var $grid= $('#'+ rootid +" .dg-detail");	
				var _param = {form:$form,grid:$grid,url:'/'+sysmanMain.getRootPath()+detailSaveUrl}
				
				if(fullBizcode)				
					_param.fullBizcode = fullBizcode	
					
				var validateStatus =$form.form('enableValidation').form('validate');
				if(!validateStatus){
					msgServ.Msgslide("请填写表单内容");
					return
				}
				datagridServ.save(_param)
			}
			
			,
			del:function(){
				
				var $grid= $('#'+ rootid +" .dg-detail")
				
				datagridServ.delRow({url:'/'+sysmanMain.getRootPath()+detailDelUrl,target:$grid});
			
			
			}
			,
			cancel:function(){
				$('#'+window_id).window('close');
			}
			,
			/**
			 * 执行查询
			 * @param _options.target
			 */
			doSearch:function(_options){
				
			}
		}
		var buttonConfig={
				"新增"     : btnEvent.add,
			    "编辑"     : btnEvent.edit,
				"删除"     : btnEvent.del	,
				"取消"     : btnEvent.cancel	,
				"保存"     : btnEvent.save,
				"导出当前页": function(){btnEvent.exportExcel()},
				"导出全部"  : function(){btnEvent.exportExcel({type:'all'})},
				'批量退回'  : function(){btnEvent.auditList({status:-1})},
				'批量审核'  : function(){btnEvent.auditList({status:1})},
				'批量提交'  : function(){btnEvent.auditList({status:0})},				
				"执行查询"  : btnEvent.doSearch
			}
		var private_method = {
			init:function(){
				rootpath = '/'+sysmanMain.getRootPath();
				
				$.ajax({
					url : rootpath + '/permission/query/getOneByField.action',
					data: {field:'bizcode',fieldVal:bizcode},
					async: false,
					success:function(data){
						if(data && data.fullBizcode){
							fullBizcode = data.fullBizcode
						}
					}
				})
				
				
			}
			,
			//初始化树结构
			initTree:function(){
				if(window.console)
					console.log('initTree ...');
				var _el = $('#'+rootid + ' .dg-type')
				if(_el.length==1){
					var _param = {
						url			 : rootpath + typeQryUrl,
						height		 : gridHeight,
						title		 : '供应商',
						rownumbers   : true,
						animate      : true,									
						striped      : true,
						border       : true,
						//singleSelect:false,
						idField      : 'id',
						treeField    : 'name',					
						fitColumns   : true,
						checkOnSelect: true,						
						selectOnCheck: true,
						columns		 : [[
							{field:'name',title:'供应商名称',halign:'center',width:30}						
						]],
						
						onClickRow	 : private_method.refreshDetail
					}
					
					_el.treegrid(_param);
				}
				
			}
			,
			
			refreshDetail:function(node){
				curTreeNode = node
				$('#'+rootid + ' .dg-detail').datagrid('load',{condition:{supplyid:node.id}})
			}
			,
			//初始化明细列表
			initDetail:function(){
				
				var _el = $('#'+rootid + ' .dg-detail')
				if(window.console)
					console.log('initDetail ...');
				if(_el.length==1){
					
					//自定义按钮
					var listTd = $('#'+rootid + ' .list-btn')
					if(listTd.length==1){					
						
						if(fullBizcode){								
							//初始化按钮功能
							var _param = {
								bizcode  : fullBizcode,
								rootPath : rootpath,
								container: listTd,
								callback : private_method.bindBtnEvent}
							btnServ.generateListBtn(_param);
						}											
					}
				
					// 搜索框
					var $searchbox=$('#'+ rootid +" .searchbox-detail")
					if($searchbox.length>0){
						
						var _searchParam = {
							prompt : '模糊查询',
							searcher : function(value, name) {
								var obj = {};
								obj.searchName=name;
								obj.searchValue=value;							
		//						private_method.doSearch(obj)
						}}				
						$($searchbox).searchbox(_searchParam);
					}
					//初始化列表
					var _param = {
						url			 : rootpath + detailQryUrl,
						height		 : gridHeight,
						title		 : '耗材清单',
						rownumbers	 : true,
						animate		 : true,									
						striped		 : true,
						border		 : true,
						//singleSelect:false,
						idField		 : 'id',									
						fitColumns	 : true,
						checkOnSelect: true,
						pagination 	 : true,
						selectOnCheck: true,
						onDblClickRow: private_method.detailDblClick,
						toolbar      : $('#'+ rootid + ' .tb-detail'),
						columns:[[
							{field:'ck',checkbox:"true"},        
//							{field:'name',title:'耗材商品名',halign:'center',width:100},        
							{field:'name',title:'耗材通用名',width:100,align:'center',halign:'center'},
							{field:'specification',title:'规格型号',halign:'center',width:30},        
							{field:'unit',title:'计量单位',width:30,align:'center',halign:'center'},
							{field:'price',title:'进货单价',halign:'center',width:30},        
							{field:'phc_code',title:'流水号',halign:'center',width:30},        
							{field:'supplyname',title:'供应商',width:30,align:'center',halign:'center'},							
							{field:'isqianyuan',title:'是否千元耗材',width:30,align:'center',halign:'center'}
						]]
						
					}
					_el.datagrid(_param);					
				}
			}
			,
			
			//初始化列表按钮
			initDetailBtn:function(){
				
			}
			,
			//初始化编辑页面
			initEdit:function(){
				var $grid= $('#'+ rootid +" .dg-detail")					
				
				// 清除表单									
				var $form = $('#' +window_id + ' .main-form')
				$form.form('clear')
				
				
				var record = $grid.datagrid("getSelected")
				
				if(!record){
					record = {
						supplyname    : curTreeNode.text,
						supplyid      : curTreeNode.id,
						supplycode    : curTreeNode.dwxh,
						producetype   : '国产',
						isqianyuan	  : '否'
					}					
				}				
				
				$form.form('load',record);
				// 编辑窗口初始化					
				var editTd = $('#' +window_id + ' .edit-btn')
				
				
				if(editTd && editTd.length>0){
					var _param = {
						bizcode  : fullBizcode,
						rootPath : rootpath,
						container: editTd,
						callback : private_method.bindEditEvent}
					btnServ.generateEditBtn(_param);
				}
			}
			,
			//树结构点击事件
			typeClick:function(){
				
			}
			,
			//生成拼音码
			generatePY:function(){
				
			}
			,
			//列表双击事件
			detailDblClick:function(rowIndex, rowData){
				var $grid= $('#'+ rootid +" .dg-detail")
				$grid.datagrid('clearSelections').datagrid('checkRow',rowIndex)
				btnEvent.editExecute(true);				
			}
			,
			//绑定列表页面按钮事件
			bindBtnEvent:function(e){
				
				$.messager.progress('close')
				// 绑定按钮事件
				var btn = $('#'+rootid + ' .tb-list .function-btn')				
				
				//判断按钮是否存在
				if(btn.length>0){
					$(btn).unbind('click');
					$(btn).bind('click', function(e){ btnServ.applyButton({buttonEvt:e,buttonConfig:buttonConfig})})	
				}
				
				//执行查询按钮事件绑定				
				btn = $('#'+rootid + ' .condition-form .conditon-search')
				if(btn.length>0){
					$(btn).unbind('click');
					$(btn).bind('click', function(e){ btnServ.applyButton({buttonEvt:e,buttonConfig:buttonConfig})})	
				}				
			}			
			,
			//绑定编辑页面按钮事件
			bindEditEvent:function(e){
				// 绑定按钮事件
				var btn = $('#'+window_id + ' .function-btn')				
				
				//判断按钮是否存在
				if(btn.length>0){
					$(btn).unbind('click');
					$(btn).bind('click', function(e){ btnServ.applyButton({buttonEvt:e,buttonConfig:buttonConfig})})	
				}
			}
			,
			
			
			initWH:function(){
				var _currentTab = $('#centerTabs').tabs('getSelected')
	        	pageWidth = $(_currentTab).width() 
	        	pageHeight = $(_currentTab).height() -50
	        	
	        	gridHeight = pageHeight - $('#'+rootid + ' .well').height()
			}
			
		}
		
		return public_method;
		
})